/* 
 * Copyright 2014 ShangDao.Ltd  All rights reserved.
 * SiChuan ShangDao.Ltd PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * 
 * @ActOfficialNews.java  2014-9-18 下午4:18:29 - Carson
 * @author Guzhu
 * @email:muyi126@163.com
 * @version 1.0
 */

package com.android.tiango.baba.act;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.scsd.tiango.R;
import com.android.support.httpclient.HttpCallBack;
import com.android.support.httpclient.HttpClientAsync;
import com.android.support.httpclient.HttpParams;
import com.android.tiango.baba.BaListActivity;
import com.android.tiango.baba.cookie.HttpUrl;
import com.android.tiango.baba.cookie.ShareCookie;
import com.android.tiango.baba.entity.MessageEnity;
import com.android.tiango.baba.entity.MessageModel;
import com.android.tiango.baba.util.Constants;
import com.android.tiango.baba.util.ImageLoader;
import com.android.tiango.baba.util.StringUtils;
import com.android.tiango.baba.util.TimeToUtil;
import com.android.tiango.baba.util.ToastUtils;
import com.android.tiango.baba.view.MeasureListView;
import com.android.tiango.baba.view.PullListView.PullListViewListener;
import com.android.tiango.baba.view.RoundCornerImageView;
import com.android.tiango.baba.view.RoundImageView;

/**
 * 官方消息
 * @author YW_pc
 * 
 */
public class ActOfficialNews extends BaListActivity<MessageModel> implements PullListViewListener {

	private String messageId;
	private String msgType;
	private String fromUserId;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		MessageModel messagemodel=(MessageModel) getIntent().getExtras().get(Constants.DATA);
		if(messagemodel==null){
			finish();
		}
		messageId=messagemodel.getMessageId();
		msgType=messagemodel.getMsgType();
		fromUserId=messagemodel.getFromUserId();
		mImageLoader=new ImageLoader(this, R.drawable.temp_photo);
		String name=messagemodel.getNickName();
		
		setTitleName(name);
		mTitleBar.setBackIcon(R.drawable.back);
		setBackOnClickListener();
		
		mListView.setPullListener(this);
		mListView.startOnRefresh();
	}

	@Override
	protected View getItemView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (null == convertView) {
			convertView = LayoutInflater.from(this).inflate(R.layout.item_officialnews, null);
			holder = new ViewHolder();
			holder.tv_actinfo = (TextView) convertView.findViewById(R.id.tv_actinfo);
			//holder.iv_img = (RoundCornerImageView) convertView.findViewById(R.id.iv_img);
			holder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
			holder.iv_acthead = (RoundImageView) convertView.findViewById(R.id.iv_acthead);
			holder.mlistview=(MeasureListView) convertView.findViewById(R.id.lv_img);
			holder.iv_line=(ImageView) convertView.findViewById(R.id.iv_line);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		if(position==mAdapter.getCount()-1){
			holder.iv_line.setVisibility(View.INVISIBLE);
		}else{
			holder.iv_line.setVisibility(View.VISIBLE);
		}
		MessageModel bean=mAdapter.getItem(position);
		String headimg=bean.getHeadImg();
		String createTime=bean.getCreateTime();
		String content=bean.getContent();
		String messageimg=bean.getMessageImages();
		String[] img=messageimg.split(",");
		
		holder.tv_actinfo.setText(content);
		String time=TimeToUtil.getMessageDayFormat(Long.valueOf(createTime));
		holder.tv_time.setText(time);
		mImageLoader.loadImage(headimg, holder.iv_acthead);
		
		
		if(StringUtils.isEmpty(messageimg)){
			holder.mlistview.setVisibility(View.GONE);
		}else{
			holder.mlistview.setVisibility(View.VISIBLE);
			MessageImgAdapter imgAdapter=new MessageImgAdapter(img,this);
			holder.mlistview.setAdapter(imgAdapter);
		}
		
		return convertView;
	}

	class ViewHolder {
		private TextView tv_actinfo;
		private ImageView iv_line;
		private TextView tv_time;
		private RoundImageView iv_acthead;
		private MeasureListView mlistview;
	}
	public class MessageImgAdapter extends BaseAdapter{

		private String[] img;
		private Context context;
		public MessageImgAdapter(String[] img,Context context){
			this.img=img;
			this.context=context;
		}
		
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return img.length;
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup arg2) {
			// TODO Auto-generated method stub
			ViewHolder viewholder=null;
			if(convertView==null){
				viewholder=new ViewHolder();
				convertView=LayoutInflater.from(context).inflate(R.layout.item_guanfang_message, null);
				viewholder.img=(RoundCornerImageView) convertView.findViewById(R.id.iv_img);
				convertView.setTag(viewholder);
			}else{
				viewholder=(ViewHolder) convertView.getTag();
			}
			mImageLoader.loadImage(img[position], viewholder.img);
			return convertView;
		}
		class ViewHolder{
			private RoundCornerImageView img;
		}
		
	}
	@Override
	public void onPullRefresh() {
		// TODO Auto-generated method stub
		mPageIndex=1;
		loadMessage();
	}

	
	@Override
	public void onPullLoadMore() {
		// TODO Auto-generated method stub
		mPageIndex+=1;
		loadMessage();
	}
	private void loadMessage() {
		// TODO Auto-generated method stub
		
		HttpClientAsync client = HttpClientAsync.getInstance();
		HttpParams params = new HttpParams();
		params.put("token", ShareCookie.getToken());
		params.put("messageId", messageId);
		params.put("msgType", msgType);
		params.put("fromUserId", fromUserId);
		params.put("pageIndex", mPageIndex);
		params.put("pageSize", Constants.PAGE_SIZE);
		client.get(HttpUrl.getUrl(HttpUrl.MESSAGEINFO), params, new HttpCallBack() {
			@Override
			public void onHttpSuccess(Object ob) {
				System.out.println("ob :"+ob);
				dismissWaitingDialog();
				MessageEnity messageenity=(MessageEnity) ob;
				if(null!=messageenity){
					mAdapter.putData(messageenity.getData());
				}
				
			}
			@Override
			public void onHttpStarted() {
				showWaitingDialog("正在加载...");
			}
			@Override
			public void onHttpFailure(Exception arg0, String arg1) {
				dismissWaitingDialog();
				if(mPageIndex>1){
					mPageIndex-=1;
				}
				if(!StringUtils.isEmpty(arg1)){
					ToastUtils.showMessage(arg1);
				}else{
					ToastUtils.showMessage("抱歉,请重新尝试");
					
				}
			}
		}, MessageEnity.class);//
	}


}
